package org.zyl.util.time;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.zyl.util.validate.ValidateUtil;

/**
 * 时间格式转换工具类
 * 
 * @author yzzhouyalei@foxmail.com
 * @time 2017-03-23 10:10:37
 */
public class DateTimeUtils {
	public static final String pattern_yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";
	public static final String pattern_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
	public static final String pattern_yyyy_MM_dd_HH_mm_ss_SSS = "yyyy-MM-dd HH:mm:ss:SSS";
	public static final String pattern_yyyyMMddHHmmss = "yyyyMMddHHmmss";
	public static final String pattern_yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
	public static final String pattern_yyyy_MM_dd = "yyyy-MM-dd";
	public static final String pattern_MM_dd_HH_mm = "MM-dd HH:mm";
	public static final String pattern_yyyy_MM = "yyyy-MM";
	public static final String pattern_yyyyMM = "yyyyMM";
	public static final String pattern_HH_mm = "HH:mm";
	public static final String pattern_yyyyMMdd = "yyyyMMdd";
	public static final String pattern_yyMMdd = "yyMMdd";
	public static final String pattern_HHmmssSSS = "HHmmssSSS";
	public static final String[] dayNames = new String[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

	public static Date parseStringToDate(String pattern, String dateString) {
		DateFormat df1 = new SimpleDateFormat(pattern);
		Date date = null;
		try {
			date = df1.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return String.valueOf(calendar.get(Calendar.YEAR));
	}

	public static String getMonthDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		return String.valueOf(calendar.get(Calendar.MONTH) + 1) + String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
	}

	public static String formatDateToString(String pattern, Date date) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	public static Date formatStringToDate(String pattern, String time) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);

		return sdf.parse(time);
	}

	/**
	 * 根据日期获取24小时前的时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getBeforeOneDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MINUTE, -1440);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 根据日期获取90分钟(5400秒)前的时间
	 */
	public static Date getNinetyMinutesLater(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.SECOND, -5400);
		return calendar.getTime();
	}

	/**
	 * 根据日期获取90分钟(5400秒)后的时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getNinetyMinutesAfter(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.SECOND, 5400);
		return calendar.getTime();
	}

	/**
	 * 获取时间 为几分钟前
	 * 
	 * @author yzzhouyalei@foxmail.com
	 * @time 2017-03-10 18:31:43
	 */
	public static String getFriendlytime(Date d) {
		long delta = (System.currentTimeMillis() - d.getTime()) / 1000;
		if (delta <= 0) {
			return delta + "";
		}
		if (delta / (60 * 60 * 24 * 365) > 0) {
			return delta / (60 * 60 * 24 * 365) + "年前";
		}
		if (delta / (60 * 60 * 24 * 30) > 0) {
			return delta / (60 * 60 * 24 * 30) + "个月前";
		}
		if (delta / (60 * 60 * 24 * 7) > 0) {
			return delta / (60 * 60 * 24 * 7) + "周前";
		}
		if (delta / (60 * 60 * 24) > 0) {
			return delta / (60 * 60 * 24) + "天前";
		}
		if (delta / (60 * 60) > 0) {
			return delta / (60 * 60) + "小时前";
		}
		if (delta / (60) > 0) {
			return delta / (60) + "分钟前";
		}
		return "刚刚";
	}

	/**
	 * 根据日期获取年龄
	 * 
	 * @author yzzhouyalei@foxmail.com
	 * @time 2017-04-21 15:35:42
	 */
	public static int getAge(Date birthDay) throws Exception {
		if (birthDay == null) {
			return 0;
		}

		Calendar cal = Calendar.getInstance();

		if (cal.before(birthDay)) {
			throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
		}
		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH);
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
		cal.setTime(birthDay);

		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH);
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

		int age = yearNow - yearBirth;

		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				if (dayOfMonthNow < dayOfMonthBirth)
					age--;
			} else {
				age--;
			}
		}
		return age;
	}

	/**
	 * 判断now时间是否在startTime和endTime之间 是返回true 否返回false
	 */
	public static boolean isBetweenEnd(String now, String startTime, String endTime) {
		if (!ValidateUtil.isNotBlank(now) || !ValidateUtil.isNotBlank(startTime) || !ValidateUtil.isNotBlank(endTime)) {
			return false;
		}
		Date dateNow = parseStringToDate(pattern_yyyy_MM_dd_HH_mm_ss, now);
		Date dateStart = parseStringToDate(pattern_yyyy_MM_dd_HH_mm_ss, startTime);
		Date dateEnd = parseStringToDate(pattern_yyyy_MM_dd_HH_mm_ss, endTime);
		if (dateNow.getTime() >= dateStart.getTime() && dateNow.getTime() <= dateEnd.getTime()) {
			return true;
		}
		return false;
	}

	/**
	 * 时间比较 endTime大于等于startTime返回true
	 */
	public static boolean compareTime(String startTime, String endTime) {
		if (!ValidateUtil.isNotBlank(startTime) || !ValidateUtil.isNotBlank(endTime)) {
			return false;
		}

		Date dateStart = parseStringToDate(pattern_yyyy_MM_dd_HH_mm_ss, startTime);
		Date dateEnd = parseStringToDate(pattern_yyyy_MM_dd_HH_mm_ss, endTime);
		return dateEnd.getTime() >= dateStart.getTime();
	}

	/**
	 * 获取周一日期
	 */
	public static String getMondayByDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}

		cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值

		return DateTimeUtils.formatDateToString(pattern_yyyy_MM_dd, cal.getTime());
	}

	/**
	 * 获取周日日期
	 */
	public static String getSundayByDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}

		cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		cal.add(Calendar.DATE, 7 - day + 1);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值

		return DateTimeUtils.formatDateToString(pattern_yyyy_MM_dd, cal.getTime());
	}
}
